package com.xtzn.business.impl;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xtzn.business.IPersonInfoBusiness;
import com.xtzn.click.vo.CSResponse;
import com.xtzn.common.Constant;
import com.xtzn.core.enums.ResponseEnum;
import com.xtzn.mapper.entity.PersonInfo;
import com.xtzn.service.IPersonInfoService;
import com.xtzn.vo.DataRequest;

/**
 * 功能描述: <br>
 * **
 * 
 * @author zyw
 * @version [版本号, 2018年4月16日]
 */
@Component
public class PersonInfoBusinessImpl implements IPersonInfoBusiness {

	private static final Logger log = LoggerFactory.getLogger(PersonInfoBusinessImpl.class);
	
	@Autowired
	private IPersonInfoService personInfoService;
	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月16日]
	 */
	@Override
	public CSResponse retrievePersonInfoByPageIndex(DataRequest dataRequest) {
		if(dataRequest.getDateRange()!=null&&!dataRequest.getDateRange().equals("")){
			String dateRange = dataRequest.getDateRange();
			String[] dateRanges = dateRange.split("~");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date startTime = sdf.parse(dateRanges[0]);
				Date endTime = sdf.parse(dateRanges[1]);
				dataRequest.setStartTime(startTime);
				dataRequest.setEndTime(endTime);							
				
			} catch (ParseException e) {
				log.info("时间错误");
			}
		}
		dataRequest.setPageSize(Constant.pageSize);
		dataRequest.setSkips((dataRequest.getPageIndex()-1)*dataRequest.getPageSize());
		List<Map<String,Object>> list = personInfoService.selectPersonInfoByPageIndex(dataRequest);
		if(!list.isEmpty()){
			Map<String,Object> map = new HashMap<String, Object>();	
			Integer count  = personInfoService.selectCount(dataRequest);
			map.put("data", list);
			map.put("count", count);
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),(Serializable)map);
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月16日]
	 */
	@Override
	public CSResponse retrievePersonInfoById(Integer id) {
		PersonInfo personInfo = personInfoService.selectPersonInfoById(id);
		if(null!=personInfo){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),(Serializable)personInfo);
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月16日]
	 */
	@Override
	public CSResponse createPersonInfo(PersonInfo personInfo) {
		int i = personInfoService.insertPersonInfo(personInfo);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月16日]
	 */
	@Override
	public CSResponse deleteSinglePersonInfo(Integer id) {
		int i = personInfoService.deleteById(id);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月16日]
	 */
	@Override
	public CSResponse deleteBatchPersonInfo(List<Integer> list) {
		int i = personInfoService.deleteBatchById(list);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月16日]
	 */
	@Override
	public CSResponse modifyPersonInfoContent(PersonInfo personInfo) {
		int i = personInfoService.updatePersonInfoContent(personInfo);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月23日]
	 */
	@Override
	public CSResponse deleteBatchByCondition(DataRequest dataRequest) {
		if(dataRequest.getDateRange()!=null&&!dataRequest.getDateRange().equals("")){
			String dateRange = dataRequest.getDateRange();
			String[] dateRanges = dateRange.split("~");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date startTime = sdf.parse(dateRanges[0]);
				Date endTime = sdf.parse(dateRanges[1]);
				dataRequest.setStartTime(startTime);
				dataRequest.setEndTime(endTime);							
				
			} catch (ParseException e) {
				log.info("时间错误");
			}
		}
		int i = personInfoService.deleteBatchByCondition(dataRequest);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

}
